package com.i_art_engineer.meigong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.i_art_engineer.meigong.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ImageThumbListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private Context mContext;

    private int viewHeight;

    private List<String> mImages = new ArrayList<>();
    private List<String> mSelected = new ArrayList<>();

    public ImageThumbListAdapter(Context context, int height) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewHeight = height;
    }

    /**
     * 选择某个图片，改变选择状态
     *
     * @param image
     */
    public void select(String image) {
        if (mSelected.contains(image)) {
            mSelected.remove(image);
        } else {
            mSelected.add(image);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消选择某个图片，改变选择状态
     *
     * @param image
     */
    public void unselect(String image) {
        if (mSelected.contains(image)) {
            mSelected.remove(image);
            notifyDataSetChanged();
        }
    }

    /**
     * 通过图片路径设置默认选择
     *
     * @param resultList
     */
    public void setDefaultSelected(ArrayList<String> resultList) {
        for (String path : resultList) {
            if (path != null) {
                mSelected.add(path);
            }
        }
        if (mSelected.size() > 0) {
            notifyDataSetChanged();
        }
    }

    /**
     * 设置数据集
     *
     * @param images
     */
    public void setData(List<String> images) {
        mSelected.clear();

        if (images != null && images.size() > 0) {
            mImages = images;
        } else {
            mImages.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mImages.size();
    }

    @Override
    public String getItem(int i) {
        return mImages.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.list_item_thumb, viewGroup, false);
            holder = new ViewHolder(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (holder != null) {
            holder.bindData(getItem(i));
        }

        return view;
    }

    class ViewHolder {

        ImageView image;
        View mask;

        ViewHolder(View view) {
            image = (ImageView) view.findViewById(R.id.image);
            mask = view.findViewById(R.id.mask);
            view.setTag(this);
        }

        void bindData(final String data) {
            if (data == null) return;
                // 处理单选和多选状态
                if (mSelected.contains(data)) {
                    // 设置选中状态
                    mask.setVisibility(View.VISIBLE);
                } else {
                    // 未选择
                    mask.setVisibility(View.GONE);
                }
            File imageFile = new File(data);
            if (imageFile.exists()) {
                // 显示图片
                Picasso.with(mContext)
                        .load(imageFile)
                        .placeholder(R.drawable.tu)
                        .resize(viewHeight, viewHeight)
                        .centerCrop()
                        .into(image);
            } else {
                image.setImageResource(R.drawable.tu);
            }
        }

    }

}
